package com.ruoyi.creditlife.service.impl;

import com.ruoyi.creditlife.domain.*;
import com.ruoyi.creditlife.mapper.*;
import com.ruoyi.creditlife.service.ICreditLifeBehaviorService;
import com.ruoyi.creditlife.service.IReturnBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ReturnBookServiceImpl implements IReturnBookService {
 @Autowired
    private BookBorrrowRecordMapper bookBorrrowRecordMapper;
 @Autowired
    private CreditLifeBehaviorMapper creditLifeBehaviorMapper;
 @Autowired
    private BookBorrowTrackingMapper bookBorrowTracking;
 @Autowired
    private ComplianceRecordMapper complianceRecordMapper;
 @Autowired
    private ICreditLifeBehaviorService creditLifeBehaviorService;
 @Autowired
    private BookInfoMapper bookInfoMapper;
    @Override
    public boolean ReturnBook(BookBorrrowRecord borrrowRecord) {
        if(borrrowRecord.getStatus().equals("未归还")){        //正常还书
            borrrowRecord.setReturnTime(new Date());
            borrrowRecord.setIsOverdue((long)1);
            borrrowRecord.setCreditImpact((long)2);
            borrrowRecord.setStatus("已结束");
            BookBorrowTracking borrowTracking = bookBorrowTracking.selectBookBorrowTrackingByBorrowId(borrrowRecord.getId());
            CreditLifeBehavior behavior = creditLifeBehaviorMapper.selectCreditLifeBehaviorById(borrowTracking.getBehaviorId());
            behavior.setEndTime(new Date());
            behavior.setStatus("已完成");
            behavior.setCreditImpact((long)2);
            creditLifeBehaviorMapper.updateCreditLifeBehavior(behavior);
            ComplianceRecord complianceRecord = new ComplianceRecord();
            complianceRecord.setUserId(behavior.getUserId());
            complianceRecord.setRecordType((long)1);
            complianceRecord.setDescription("图书借阅");
            complianceRecord.setAmount(behavior.getAmount());
            complianceRecord.setDueTime(borrrowRecord.getDueTime());
            complianceRecord.setCompleteTime(new Date());
            complianceRecord.setStatus("已完成");
            complianceRecord.setCreditImpact((long)2);
            complianceRecordMapper.insertComplianceRecord(complianceRecord);
            creditLifeBehaviorService.ImpactCreditScore(borrrowRecord.getUserId(),2);
            BookInfo bookInfo =bookInfoMapper.selectBookInfoByBookId(borrrowRecord.getBookId());
            bookInfo.setBookNum(bookInfo.getBookNum()+1);
            bookInfoMapper.updateBookInfo(bookInfo);
        }
        else {      //逾期还书
                borrrowRecord.setReturnTime(new Date());
                borrrowRecord.setCreditImpact(borrrowRecord.getCreditImpact()+2);
                BookBorrowTracking borrowTracking = bookBorrowTracking.selectBookBorrowTrackingByBorrowId(borrrowRecord.getId());
                CreditLifeBehavior behavior = creditLifeBehaviorMapper.selectCreditLifeBehaviorById(borrowTracking.getBehaviorId());
                behavior.setEndTime(new Date());
                behavior.setCreditImpact(behavior.getCreditImpact()+2);
                creditLifeBehaviorService.ImpactCreditScore(behavior.getUserId(),2);
                creditLifeBehaviorMapper.updateCreditLifeBehavior(behavior);
                BookInfo bookInfo =bookInfoMapper.selectBookInfoByBookId(borrrowRecord.getBookId());
                bookInfo.setBookNum(bookInfo.getBookNum()+1);
                bookInfoMapper.updateBookInfo(bookInfo);
        }

        return bookBorrrowRecordMapper.updateBookBorrrowRecord(borrrowRecord)>0;
    }
}
